package com.zzh.lib.core.loader;


import android.content.Context;
import android.os.Build;
import android.provider.MediaStore;
import android.text.TextUtils;

import com.zzh.lib.core.HLibrary;

import androidx.annotation.NonNull;
import androidx.loader.content.CursorLoader;

import static android.provider.MediaStore.MediaColumns.MIME_TYPE;

/**
 * Created by ZZH on 2023/2/1.
 *
 * @date: 2023/2/1 21:55
 * @email: zzh_hz@126.com
 * @author: zzh
 * @description: com.zzh.lib.core.loader
 */
public class HImageLoader extends CursorLoader {
    private static final String[] IMAGE_PROJECTION = {
            //Base File
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.TITLE,
            MediaStore.Images.Media.DATA,
            MediaStore.Images.Media.SIZE,
            MediaStore.Images.Media.BUCKET_ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.DISPLAY_NAME,
            MIME_TYPE,
            //Image File
            MediaStore.Images.Media.ORIENTATION
    };

    public HImageLoader(@NonNull Context context) {
        super(context);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            setProjection(new String[]{
                    //Base File
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.TITLE,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Images.Media.DATE_ADDED,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.AUTHOR,
                    MIME_TYPE,
                    //Image File
                    MediaStore.Images.Media.ORIENTATION
            });
        } else {
            setProjection(new String[]{
                    //Base File
                    MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.TITLE,
                    MediaStore.Images.Media.DATA,
                    MediaStore.Images.Media.SIZE,
                    MediaStore.Images.Media.BUCKET_ID,
                    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
                    MediaStore.Images.Media.DATE_ADDED,
                    MediaStore.Images.Media.DISPLAY_NAME,
                    MIME_TYPE,
                    //Image File
                    MediaStore.Images.Media.ORIENTATION
            });
        }


        setUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        setSortOrder(MediaStore.Images.Media.DATE_ADDED + " DESC");

        if (TextUtils.isEmpty(HLibrary.getImageSelection())) {
            setSelection(MIME_TYPE + "=? or " + MIME_TYPE + "=? or " + MIME_TYPE + "=? or " + MIME_TYPE + "=?");
        } else {
            setSelection(HLibrary.getImageSelection());

        }

        if (HLibrary.getImageSelectionArgs() == null) {
            setSelectionArgs(new String[]{"image/jpeg", "image/png", "image/jpg", "image/gif"});
        } else {
            setSelectionArgs(HLibrary.getImageSelectionArgs());
        }

    }


}
